﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Fast.Framework.CustomAttribute
{

    /// <summary>
    /// 不为空或空字符串属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class NotNullOrEmptyAttribute : ValidationAttribute
    {

        /// <summary>
        /// 是否验证
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="validationContext">验证上下文</param>
        /// <returns></returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (string.IsNullOrWhiteSpace(Convert.ToString(value)))
            {
                if (string.IsNullOrWhiteSpace(ErrorMessage))
                {
                    ErrorMessage = $"{validationContext.DisplayName} NotNullOrEmpty";
                }
                return new ValidationResult(ErrorMessage);
            }
            return ValidationResult.Success;
        }
    }
}
